package com.plantpoint.service.impl;

import com.plantpoint.service.UtilsService;
import com.plantpoint.dao.UtilsDao;
import com.plantpoint.entity.bean.Image;
import com.plantpoint.util.IdWorker;
import com.plantpoint.util.UploadImageUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ATSELI
 * @version 1.0
 * @date 2021/4/24 10:59
 */

@Service
public class UtilsServiceImpl implements UtilsService {
    private final UtilsDao dao;
    private final IdWorker idWorker;
    private final UploadImageUtil img;

    public UtilsServiceImpl(UtilsDao dao, IdWorker idWorker, UploadImageUtil img) {
        this.dao = dao;
        this.idWorker = idWorker;
        this.img = img;
    }

    @Override
    public Image potted(MultipartFile image, String pId, HttpServletRequest request){
        String openid = request.getParameter("openid");
        String id = idWorker.nextId() + "";
        Image realImg = img.addImg(image, "15", pId);
        // Image realImg = img.addImg(image, openid, pId);
        realImg.setId(id);
        int insert = dao.insert(realImg);
        if (insert == 1){
            return realImg;
        }else {
            return null;
        }
    }

    @Override
    public Image entry(MultipartFile image, String eId){
        String id = idWorker.nextId() + "";
        Image realImg = img.addImg(image, null, eId);
        realImg.setId(id);
        int insert = dao.insert(realImg);
        if (insert == 1){
            return realImg;
        }else {
            return null;
        }
    }
}
